package com.fzu.mmdbproxyframework.cluster.passcloud;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class InternetwareTool {
	// 将json转换为map
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> JSON2Map(JSONObject o) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		for (Object k : o.keySet()) {
			if ("a".equals(k.toString()) || "b".equals(k.toString())
					|| "c".equals(k.toString()))
				continue;
			Object v = o.get(k);
			if (v instanceof JSONArray) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Iterator<JSONObject> it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(JSON2Map(json2));
				}
				if ("a".equals(k.toString()) || "b".equals(k.toString())
						|| "c".equals(k.toString()))
					continue;
				map.put(k.toString(), list);
			} else {
				map.put(k.toString(), v);
			}
		}
		return map;
	}

	
	// 获取一个空闲的 IP
	public static String getFreeIpInBasicZoneInternetware(String zoneId) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("zoneid", zoneId);
		String result = SendRequest.sendRequest(InternetwareCmd.GetFreeIpCmd,
				params);
		if (null == result) {
			return null;
		}
		JSONObject json = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.GetFreeIpRes).getJSONObject("getfreeip");
		if (null == json) {
			return null;
		}
		return json.getString("displaytext");
	}

	// 获取主机、存储分级
	public static JSONObject listTaxonomistInternetware(
			HashMap<String, String> params) {
		String result = SendRequest.sendRequest(
				InternetwareCmd.ListTaxonomistCmd, params);
		if (null == result) {
			return null;
		}
		JSONObject json = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListTaxonomistRes);
		if (null == json) {
			return null;
		}
		return json;
	}

	// 获取当前数据中心下所有主机的 CPU speed
	public static HashMap<String, Object> listHostResourceLimitInternetware(
			String hypervisor, String zoneId) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("hypervisor", hypervisor);
		params.put("zoneid", zoneId);
		String result = SendRequest.sendRequest(
				InternetwareCmd.ListHostResourceLimitCmd, params);
		if (null == result) {
			return null;
		}
		JSONObject json = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListHostResourceLimitRes).getJSONObject(
				"hostRerourceLimitInternetware");
		if (null == json) {
			return null;
		}
		return JSON2Map(json);
	}

	// 获取操作系统列表
	public static ArrayList<HashMap<String, Object>> listOstypes(
			HashMap<String, String> params) {
		ArrayList<HashMap<String, Object>> ostypes = new ArrayList<HashMap<String, Object>>();
		String result = SendRequest.sendRequest(InternetwareCmd.ListOsTypesCmd,
				params);
		if (null == result) {
			return null;
		}
		JSONArray json = JSONObject.fromObject(result).getJSONObject(
				InternetwareCmd.ListOsTypesRes).getJSONArray("ostype");
		if (null == json) {
			return null;
		}
		for (int i = 0; i < json.size(); i++) {
			JSONObject o = json.getJSONObject(i);
			ostypes.add(JSON2Map(o));
		}
		return ostypes;
	}

	
	// 将2进制地址转换为32位的ip
	static String IntToBin(String x) {
		String ret = "";
		int temp = Integer.parseInt(x);
		for (int i = 7; i >= 0; i--) {
			if (((1 << i) & temp) != 0)
				ret += "1";
			else
				ret += "0";
		}
		return ret;
	}

	// 将32位的ip地址转换为2进制
	static String ipToBin(String s) {
		String[] ns = s.split("\\.");
		String r = "";
		for (int i = 0; i < ns.length; i++) {
			r += (IntToBin(ns[i]) + ".");
		}
		return r.substring(0, r.length() - 1);
	}

	// 输入网关和子网掩码，得到起止ip地址和ip个数
	static HashMap<String, Object> getIpRange(String gateway, String netmask) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		String netmask2Bin = ipToBin(netmask);
		String gateway2Bin = ipToBin(gateway);
		netmask2Bin = netmask2Bin.replace(".", "");
		gateway2Bin = gateway2Bin.replace(".", "");
		String gatewayFixed = gateway2Bin
				.substring(0, netmask2Bin.indexOf('0'));
		int gatewayRest = gateway2Bin.length() - netmask2Bin.indexOf('0');
		map.put("ips", (int) (Math.pow(2, gatewayRest) - 1)); // 计算ip总个数
		String startIpRest = "";
		String endIpRest = "";
		for (int i = 0; i < gatewayRest - 1; i++) {
			startIpRest += "0";
			endIpRest += 1;
		}
		startIpRest += "1";
		endIpRest += "1";
		String startIp = gatewayFixed + startIpRest;
		String endIp = gatewayFixed + endIpRest;
		startIp = new BigInteger(startIp.substring(0, 8), 2).toString() + "."
				+ new BigInteger(startIp.substring(8, 16), 2).toString() + "."
				+ new BigInteger(startIp.substring(16, 24), 2).toString() + "."
				+ new BigInteger(startIp.substring(24, 32), 2).toString();
		endIp = new BigInteger(endIp.substring(0, 8), 2).toString() + "."
				+ new BigInteger(endIp.substring(8, 16), 2).toString() + "."
				+ new BigInteger(endIp.substring(16, 24), 2).toString() + "."
				+ new BigInteger(endIp.substring(24, 32), 2).toString();
		map.put("startIp", startIp);
		map.put("endIp", endIp);
		return map;
	}
//	public static String getAccountId(String accountName){
//		HashMap<String, String> params = new HashMap<String, String>();
//		params.put("name", accountName);
//		ArrayList<HashMap<String, Object>> accounts = new AccountOperation().listAccounts(params);
//		HashMap<String, Object> account = accounts.get(0);
//		return account.get("domainid").toString();
//	}
	public static void main(String[] args) {
		// System.out.println(getFreeIpInBasicZoneInternetware("1e707ed5-8e73-4a12-a98b-553cee0cdd71"));
		// System.out.println(listHostResourceLimitInternetware("KVM","1e707ed5-8e73-4a12-a98b-553cee0cdd71"));
		// String n="255.255.255.0";
		// String g="192.168.6.1";
		// System.out.println(getIpRange(g,n));
//		System.out.println(listPublicIps(null));
//		System.out.println(getAccountId("lyp"));
	}
}
